//
//  WordTestSetView.m
//  JPWords
//
//  Created by 梁淑婷 on 2024/7/6.
//

#import "WordTestSetView.h"

@interface WordTestSetView()
@property(nonatomic,strong)NSMutableArray *selArr;
@property(nonatomic,copy)NSString *wordNumber2;
@property(nonatomic,copy)NSArray *numArr;
@property(nonatomic,assign)NSInteger num_index;
@property(nonatomic,strong)UIView *bgView;
@end

@implementation WordTestSetView

-(instancetype)initWithFrame:(CGRect)frame andWordNumber:(NSString*)wordNumber andMode:(NSString*)mode{
    self = [super initWithFrame:frame];
    if(self){
        self.wordNumber2 = wordNumber;
        NSArray *arr = [mode componentsSeparatedByString:@","];
        self.selArr = [NSMutableArray arrayWithArray:arr];
        [self createUI];
    }
    return self;
}

-(void)createUI{
    
    __weak typeof(self)weakSelf = self;
    self.backgroundColor = UIColorHex_(0x000000,0.4);
    UITapGestureRecognizer *hiddenTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(hiddenTap:)];
    [self addGestureRecognizer:hiddenTap];
    
    UIView *bgView = [[UIView alloc]init];
    bgView.backgroundColor = [UIColor whiteColor];
    bgView.layer.cornerRadius = 10;
    bgView.layer.masksToBounds = YES;
    [self addSubview:bgView];
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(@280);
        make.height.mas_equalTo(@345);
        make.centerX.mas_equalTo(weakSelf);
        make.centerY.mas_equalTo(weakSelf);
    }];
    self.bgView = bgView;
    
    UIView *topBgView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 280,110)];
    topBgView.backgroundColor = UIColorHex_(0x15CC9D,0.06);
    [bgView addSubview:topBgView];
    [topBgView addRoundedCorners:UIRectCornerTopLeft|UIRectCornerTopRight withRadii:CGSizeMake(10,10)];
    
    UILabel *tipLabel = [[UILabel alloc]init];
    tipLabel.font = JPUIFontPingFangSCMedium(17);
    tipLabel.textColor = KFontBlackColor;
    tipLabel.text = @"每组词数";
    [bgView addSubview:tipLabel];
    [tipLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(@20);
        make.top.mas_equalTo(@19);
        make.height.mas_equalTo(@24);
    }];
    
    self.numArr = @[@"5",@"10",@"15",@"20"];
    CGFloat c_left = 20;
    for (NSInteger i = 0; i < self.numArr.count; i++) {
        NSString *num = self.numArr[i];
        UIButton *countBtn = [[UIButton alloc]init];
        [countBtn setTitle:num forState:UIControlStateNormal];
        [countBtn setTitleColor:UIColorHex(0xCCCCCC) forState:UIControlStateNormal];
        [countBtn setTitleColor:KThemeColor forState:UIControlStateSelected];
        countBtn.titleLabel.font = JPUIFontInterMedium(14);
        countBtn.layer.cornerRadius = 15;
        countBtn.layer.masksToBounds = YES;
        countBtn.layer.borderWidth = 0.5;
        if([num isEqualToString:self.wordNumber2]){
            self.num_index = i;
            countBtn.selected = YES;
            countBtn.layer.borderColor = KThemeColor.CGColor;
        }else{
            countBtn.selected = NO;
            countBtn.layer.borderColor = UIColorHex(0xCCCCCC).CGColor;
        }
        countBtn.tag = 100+i;
        [countBtn addTarget:self action:@selector(countBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [bgView addSubview:countBtn];
        [countBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.height.mas_equalTo(@30);
            make.left.mas_equalTo(c_left);
            make.top.mas_equalTo(tipLabel.mas_bottom).offset(16);
        }];
        c_left = c_left+30+22;
    }
    
    
    UILabel *tipLabel2 = [[UILabel alloc]init];
    tipLabel2.font = JPUIFontPingFangSCMedium(17);
    tipLabel2.textColor = KFontBlackColor;
    tipLabel2.text = @"测试模式";
    [bgView addSubview:tipLabel2];
    [tipLabel2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(@20);
        make.top.mas_equalTo(@126);
        make.height.mas_equalTo(@24);
    }];
    
    CGFloat m_top = 12;
    NSArray *mArr = @[@"看书写判断读音和释义",@"听读音判断书写和释义",@"看释义判断书写和读音"];
    for(NSInteger i = 0;i < mArr.count;i++){
        UIButton *modeBtn = [[UIButton alloc]init];
        [modeBtn setTitle:mArr[i] forState:UIControlStateNormal];
        [modeBtn setTitleColor:UIColorHex(0xCCCCCC) forState:UIControlStateNormal];
        [modeBtn setTitleColor:KThemeColor forState:UIControlStateSelected];
        modeBtn.titleLabel.font = JPUIFontPingFangSCRegular(15);
        modeBtn.layer.cornerRadius = 33/2.0;
        modeBtn.layer.masksToBounds = YES;
        modeBtn.layer.borderWidth = 1.0;
        if([self.selArr containsObject:[NSString stringWithFormat:@"%ld",i+1]]){
            modeBtn.selected = YES;
            modeBtn.layer.borderColor = KThemeColor.CGColor;
        }else{
            modeBtn.selected = NO;
            modeBtn.layer.borderColor = UIColorHex(0xCCCCCC).CGColor;
        }
        modeBtn.tag = 1000+i;
        [modeBtn addTarget:self action:@selector(modeBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [bgView addSubview:modeBtn];
        [modeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(tipLabel2.mas_bottom).offset(m_top);
            make.height.mas_equalTo(@33);
            make.left.mas_equalTo(@20);
            make.width.mas_equalTo(@174);
        }];
        m_top = m_top+33+10;
    }
    
    UIView *line = [[UIView alloc]init];
    line.backgroundColor = KLineColor;
    [bgView addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(@-48);
        make.height.mas_equalTo(@0.5);
        make.left.right.mas_equalTo(@0);
    }];
    
    UIButton *sureBtn = [[UIButton alloc]init];
    [sureBtn setTitle:@"确定" forState:UIControlStateNormal];
    [sureBtn setTitleColor:KThemeColor forState:UIControlStateNormal];
    sureBtn.titleLabel.font = JPUIFontPingFangSCRegular(16);
    [sureBtn addTarget:self action:@selector(sureBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [bgView addSubview:sureBtn];
    [sureBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(@0);
        make.height.mas_equalTo(@48);
    }];
    
    
}

#pragma mark - 隐藏点击
- (void)hiddenTap:(UITapGestureRecognizer *)gesture {
    CGPoint touchPoint = [gesture locationInView:self];
    // 判断点击位置是否在UIImageView内
    if (!CGRectContainsPoint(self.bgView.frame,touchPoint)) {
        // 点击位置不在UIImageView内，执行相应操作
        [self removeFromSuperview];
    }
}

-(void)setMode:(NSString *)mode{
    _mode = mode;
    NSArray  *modeArr = [mode componentsSeparatedByString:@","];
    self.selArr = [NSMutableArray arrayWithArray:modeArr];
    for(NSInteger i = 0;i < 3;i++){
        UIButton *modeBtn = [self viewWithTag:1000+i];
        if([self.selArr containsObject:[NSString stringWithFormat:@"%ld",i+1]]){
            modeBtn.selected = YES;
            modeBtn.layer.borderColor = KThemeColor.CGColor;
        }else{
            modeBtn.selected = NO;
            modeBtn.layer.borderColor = UIColorHex(0xCCCCCC).CGColor;
        }
    }
}

-(void)setWordNumber:(NSString *)wordNumber{
    _wordNumber = wordNumber;
    self.wordNumber2 = wordNumber;
    for (NSInteger i = 0; i < self.numArr.count; i++) {
        NSString *num = self.numArr[i];
        UIButton *countBtn = [self viewWithTag:100+i];
        if([num isEqualToString:wordNumber]){
            self.num_index = i;
            countBtn.selected = YES;
            countBtn.layer.borderColor = KThemeColor.CGColor;
        }else{
            countBtn.selected = NO;
            countBtn.layer.borderColor = UIColorHex(0xCCCCCC).CGColor;
        }
    }
}


#pragma mark - 词数选择
-(void)countBtnClick:(UIButton*)btn{
    NSInteger index =  btn.tag-100;
    UIButton *countBtn = [self viewWithTag:self.num_index+100];
    countBtn.selected = NO;
    countBtn.layer.borderColor = UIColorHex(0xCCCCCC).CGColor;
    
    btn.selected = YES;
    btn.layer.borderColor = KThemeColor.CGColor;
    
    self.num_index = index;
    self.wordNumber2 = btn.titleLabel.text;
    
}

#pragma mark - 测试模式选择
-(void)modeBtnClick:(UIButton*)btn{
    NSInteger index = btn.tag-1000;
    NSString *mode = [NSString stringWithFormat:@"%ld",index+1];
    if([self.selArr containsObject:mode]){
        [self.selArr removeObject:mode];
        btn.selected = NO;
        btn.layer.borderColor = UIColorHex(0xCCCCCC).CGColor;
    }else{
        [self.selArr addObject:mode];
        btn.selected = YES;
        btn.layer.borderColor = KThemeColor.CGColor;
    }
}

#pragma mark - 确定
-(void)sureBtnClick{
    if(self.selArr.count == 0){
        [JPProgressHUD showMsg:@"请最少选择一种测试模式"];
        return;
    }
    NSMutableArray *sortedSelArr = [NSMutableArray arrayWithArray:self.selArr];
    // 对数组进行排序
    [sortedSelArr sortUsingComparator:^NSComparisonResult(NSString *str1, NSString *str2) {
        NSInteger num1 = [str1 integerValue];
        NSInteger num2 = [str2 integerValue];
        if (num1 < num2) {
            return NSOrderedAscending;
        } else if (num1 > num2) {
            return NSOrderedDescending;
        } else {
            return NSOrderedSame;
        }
    }];
    
    NSMutableString *modestr = [NSMutableString string];
    for(NSInteger i = 0;i < sortedSelArr.count;i++){
        NSString *mode = sortedSelArr[i];
        [modestr appendString:mode];
        if(i < sortedSelArr.count-1){
            [modestr appendString:@","];
        }
    }
    if(_sureBlock){
        _sureBlock(self.wordNumber2,modestr);
    }
}


@end
